package net.hn.hnms.biz.support.service.impl;

import net.hn.hnms.biz.support.company.domain.dto.RiskStatisticsDto;
import net.hn.hnms.biz.support.company.domain.vo.ChartVo;
import net.hn.hnms.biz.support.company.domain.vo.NowDetectionVo;
import net.hn.hnms.biz.support.company.domain.vo.TodayAlarmVo;
import net.hn.hnms.biz.support.constant.ConstantSupport;
import net.hn.hnms.biz.support.domain.SupportHisPo;
import net.hn.hnms.biz.support.domain.dto.AbscissdataNowDto;
import net.hn.hnms.biz.support.service.EarlyWarningService;
import net.hn.hnms.biz.support.service.RoofHazardService;
import net.hn.hnms.biz.support.service.RoofService;
import net.hn.hnms.biz.support.service.SupportService;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * 公司端-顶板灾害的接口实现类
 */
@Service
public class RoofHazardServiceImpl implements RoofHazardService {

    /**
     * 获取cjdy库（离层和锚杆）的服务类
     */
    @Autowired
    private RoofService roofService;


    /**
     * 获取zdsb（支架）的服务类
     */
    @Autowired
    private SupportService supportService;

    /**
     * 获取预警znjc库的服务类
     */
    @Autowired
    private EarlyWarningService earlyWarningService;
    /**
     * 实时检测
     * @return
     */
    @Override
    public NowDetectionVo nowDetection() {
        //实时检测返回数据格式
        NowDetectionVo nowDetectionVo = new NowDetectionVo();
        //支架压力异常数
        //查询预警数据库的支架预警表(查询支架预警一天内的预警次数)
        int supportPressureAnomaly =earlyWarningService.getDaySupportPressureAnomaly();
        nowDetectionVo.setSupportPressureAnomaly(String.valueOf(supportPressureAnomaly));

        //支架压力测点数
        //查询支架压力实时表所有煤矿的支架压力记录，
        int supportPressurePoint=supportService.selectAllSupportPressurePoint();
        nowDetectionVo.setSupportPressurePoint(String.valueOf(supportPressurePoint));
        //顶板离层异常数
        //查询离层异常表中的所有煤矿信息(弃用)
//        Integer roofSeparationAnomaly = roofService.getRoofSeparationAnomaly();
        //查询预警数据库的顶板预警表(查询顶板预警一天内的预警次数)
        int roofSeparationAnomaly = earlyWarningService.getRoofSeparationAnomaly();
        nowDetectionVo.setRoofSeparationAnomaly(String.valueOf(roofSeparationAnomaly));

        //顶板离层测点数
        Integer roofSeparationPoint = roofService.getRoofSeparationPoint();
        nowDetectionVo.setRoofSeparationPoint(String.valueOf(roofSeparationPoint));

        //锚杆异常数(查询锚杆异常表)
        Integer rockBoltAnomaly=roofService.getRockBoltAnomaly();
        nowDetectionVo.setRockBoltAnomaly(String.valueOf(rockBoltAnomaly));

        //锚杆测点数
        Integer rockBoltPoint= roofService.getRockBoltPoint();
        nowDetectionVo.setRockBoltPoint(String.valueOf(rockBoltPoint));

        return nowDetectionVo;
    }


    /**
     * 风险统计
     * @param riskStatisticsDto
     * @return
     */
    @Override
    public ChartVo riskStatistics(RiskStatisticsDto riskStatisticsDto) {
        //折线图返回格式
        ChartVo chartVo = null;
        //1-顶板离层报警曲线，2-支架报警曲线
        if ("1".equals(riskStatisticsDto.getLib())) {
            //顶板离层报警曲线
//             chartVo =roofService.riskStatistics(riskStatisticsDto.getNumber());
            chartVo = earlyWarningService.abscissdataRiskStatistics(riskStatisticsDto.getNumber());
            return chartVo;
        } else if ("2".equals(riskStatisticsDto.getLib())) {
            //支架报警曲线
//            chartVo =supportService.riskStatistics(riskStatisticsDto.getNumber());
            chartVo =earlyWarningService.supportRiskStatistics(riskStatisticsDto.getNumber());
        }

        return chartVo;
    }

    /**
     * 今日报警处置情况
     * @return
     */
    @Override
    public List<TodayAlarmVo> todayAlarm() {
        //生成返回数据格式
        List<TodayAlarmVo> todayAlarmVos = new ArrayList<>();
        //查询支架报警,从支架历史表中查询今天的报警信息（报警条件为压力值>=46MPa）
        List<SupportHisPo> supportHisPoList=supportService.todayAlar();
        for (SupportHisPo supportHisPo : supportHisPoList) {
            TodayAlarmVo todayAlarmVo = new TodayAlarmVo();
            //矿井
            if (StringUtils.isNotBlank(supportHisPo.getMineCode())) {
                todayAlarmVo.setMine(supportHisPo.getMineCode());
            } else {
                todayAlarmVo.setMine("---");
            }
            //报警类型
            todayAlarmVo.setAlarmType(ConstantSupport.SUPPORT);

            //报警原因
            todayAlarmVo.setAlarmCause(supportHisPo.getPointCode()+ConstantSupport.SUPPORT_ALARMCAUSE);

            //开始时间
            todayAlarmVo.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(supportHisPo.getDataTime()));


            //处置情况：True-已处置，false-未处置
            todayAlarmVo.setDispositionSituation(false);

            todayAlarmVos.add(todayAlarmVo);
        }

        //查询顶板报警,顶板数据在now表内，   离层量=深基点-浅基点  (报警条件为离层量>=50mm)

        List<AbscissdataNowDto> abscissdataNowDtoList =roofService.todayAlarm();
        for (AbscissdataNowDto abscissdataNowDto : abscissdataNowDtoList) {
            TodayAlarmVo todayAlarmVo = new TodayAlarmVo();
            //矿井
            if (StringUtils.isNotBlank(abscissdataNowDto.getMineCode())) {
                todayAlarmVo.setMine(abscissdataNowDto.getMineCode());
            } else {
                todayAlarmVo.setMine("---");
            }
            //报警类型
            todayAlarmVo.setAlarmType(ConstantSupport.SEPARATIONLAYER);

            //报警原因
            todayAlarmVo.setAlarmCause(abscissdataNowDto.getPointLocation()+ConstantSupport.SEPARATIONLAYER_ALARMCAUSE);

            //开始时间
            todayAlarmVo.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(abscissdataNowDto.getDataTime()));


            //处置情况：True-已处置，false-未处置
            todayAlarmVo.setDispositionSituation(false);

            todayAlarmVos.add(todayAlarmVo);
        }
        return todayAlarmVos;
    }

}
